/**
 * 流水线相关 API
 */

import { get, post, put, del } from '@/utils/request'
import type {
  Pipeline,
  PipelineInstance,
  CreatePipelineRequest,
  ExecutePipelineRequest,
} from '@/types'

/**
 * 获取流水线列表
 */
export function getPipelines(): Promise<Pipeline[]> {
  return get<Pipeline[]>('/api/Pipeline')
}

/**
 * 根据 ID 获取流水线
 */
export function getPipelineById(id: number): Promise<Pipeline> {
  return get<Pipeline>(`/api/Pipeline/${id}`)
}

/**
 * 创建流水线
 */
export function createPipeline(data: CreatePipelineRequest): Promise<Pipeline> {
  return post<Pipeline>('/api/Pipeline', data)
}

/**
 * 更新流水线
 */
export function updatePipeline(id: number, data: Partial<CreatePipelineRequest>): Promise<Pipeline> {
  return put<Pipeline>(`/api/Pipeline/${id}`, data)
}

/**
 * 删除流水线
 */
export function deletePipeline(id: number): Promise<void> {
  return del<void>(`/api/Pipeline/${id}`)
}

/**
 * 执行流水线
 */
export function executePipeline(
  id: number,
  data?: ExecutePipelineRequest
): Promise<PipelineInstance> {
  return post<PipelineInstance>(`/api/Pipeline/${id}/execute`, data)
}

/**
 * 获取流水线执行历史
 */
export function getPipelineHistory(id: number, limit = 10): Promise<PipelineInstance[]> {
  return get<PipelineInstance[]>(`/api/Pipeline/${id}/history`, {
    params: { limit },
  })
}

/**
 * 启用流水线
 */
export function enablePipeline(id: number): Promise<void> {
  return post<void>(`/api/Pipeline/${id}/enable`)
}

/**
 * 禁用流水线
 */
export function disablePipeline(id: number): Promise<void> {
  return post<void>(`/api/Pipeline/${id}/disable`)
}
